package com.example.deliveryapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.deliveryapplication.entity.CU
import com.example.deliveryapplication.entity.Client
import com.example.deliveryapplication.entity.SelectString
import com.example.deliveryapplication.sql.AppDatabase
import com.example.deliveryapplication.wiget.AddClientDialog
import kotlinx.android.synthetic.main.activity_client_set.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class CUActivity : AppCompatActivity() ,CenterStringListAdapter.CallBack{
    private val cuDao by lazy { AppDatabase.getDatabase(this).cuDao() }
    private val planItemDao by lazy { AppDatabase.getDatabase(this).planItemDao() }
    val mList by lazy {ArrayList<SelectString>()  }
    val mAdapter by lazy { CenterStringListAdapter(this,mList,this) }
    var selectPosition=-1
    private val mViewModel by lazy { ViewModelProvider(this)[CUSetViewModel::class.java] }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_cuactivity)

        mRecycleView.layoutManager= LinearLayoutManager(this)
        mRecycleView.adapter=mAdapter
        mConfirmBTN.setOnClickListener {
            val dialog= AddClientDialog(this)
            dialog.setListener(object : AddClientDialog.CallBack{
                override fun onConfirmClick(name: String) {
                    lifecycleScope.launch(Dispatchers.IO) {
                        var cu= CU(name=name)
                        cuDao.insertChart(cu)
                        initData()
                    }
                }

            })
            dialog.show()
        }
        initData()
        mViewModel.result.observe(this){list->
            list.forEach {
                val selectString= SelectString()
                selectString.id=it.cuId
                selectString.name=it.name.handlerNull()
                mList.add(selectString)
            }
            mAdapter.notifyDataSetChanged()
        }
        mViewModel.deleteResult.observe(this){
            when(it){
                -1->{
                    showToast("该客户已经在某个计划表中被使用，请先删除计划表中的数据")
                }
                0->{
                    showToast("删除失败")
                }
                else->{
                    showToast("删除成功")
                    mAdapter.removeAt(selectPosition,mList.size)
                    mList.removeAt(selectPosition)
                }
            }
        }
        mViewModel.updateResult.observe(this){
            initData()
        }
    }



    override fun onItemClick(position: Int, data: SelectString) {

    }

    override fun onItemDelete(position: Int, data: SelectString) {
        selectPosition=position
        data.id?.let { mViewModel.deleteModel(planItemDao,cuDao, it) }
    }

    override fun onItemEdit(position: Int, data: SelectString) {
        val dialog= AddClientDialog(this)
        dialog.setEdit(true)
        dialog.setTitle("单位")
        dialog.setData(data)
        dialog.setListener(object : AddClientDialog.CallBack{
            override fun onConfirmClick(name: String) {
                 data.id?.let {
                     val client=CU( it,name)
                    mViewModel.updateClient(cuDao,client)
                }

            }
        })
        dialog.show()
    }

    fun getData(){
        mViewModel.getAll(cuDao)
    }
    fun initData(){
        mList.clear()
        getData()
    }
}